Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DanglingPtr][wallet] Prefer non-nullable states pt.2 #26343

Merged
merged 1 commit into from
Nov 5, 2024

Conversation

cdesouza-chromium
Copy link
Collaborator

@cdesouza-chromium cdesouza-chromium commented Nov 2, 2024

This change touches several parts of wallets where we are passing pointers around that are expected to be in a valid state, and remain so for the duration of the lifetime of the object receiving them.

Historically, chromium had a guideline that required passing non-owning references as raw pointers. This has changed for a while now, and there are even supporting types like raw_ref for it. Raw pointers should be used for cases where flexibility is required about the lifetime of what is being pointed at. However, we should avoid what is in fact an anti-pattern, of passing pointers for the lifetime of an object, and then CHECKing the pointer for its validity.

This change also removes certain uses of std::unique_ptr that suffers of the same issues.

Resolves brave/brave-browser#42016

Resolves

Submitter Checklist:

  • I confirm that no security/privacy review is needed and no other type of reviews are needed, or that I have requested them
  • There is a ticket for my issue
  • Used Github auto-closing keywords in the PR description above
  • Wrote a good PR/commit description
  • Squashed any review feedback or "fixup" commits before merge, so that history is a record of what happened in the repo, not your PR
  • Added appropriate labels (QA/Yes or QA/No; release-notes/include or release-notes/exclude; OS/...) to the associated issue
  • Checked the PR locally:
    • npm run test -- brave_browser_tests, npm run test -- brave_unit_tests wiki
    • npm run presubmit wiki, npm run gn_check, npm run tslint
  • Ran git rebase master (if needed)

Reviewer Checklist:

  • A security review is not needed, or a link to one is included in the PR description
  • New files have MPL-2.0 license header
  • Adequate test coverage exists to prevent regressions
  • Major classes, functions and non-trivial code blocks are well-commented
  • Changes in component dependencies are properly reflected in gn
  • Code follows the style guide
  • Test plan is specified in PR before merging

After-merge Checklist:

Test Plan:

This change touches several parts of wallets where we are passing
pointers around that are expected to be in a valid state, and remain so
for the duration of the lifetime of the object receiving them.

Historically, chromium had a guideline that required passing non-owning
references as raw pointers. This has changed for a while now, and there
are even supporting types like `raw_ref` for it. Raw pointers should be
used for cases where flexibility is required about the lifetime of what
is being pointed at. However, we should avoid what is in fact an
anti-pattern, of passing pointers for the lifetime of an object, and
then CHECKing the pointer for its validity.

This change also removes certain uses of `std::unique_ptr` that suffers
of the same issues.

Resolves brave/brave-browser#42016
Copy link
Contributor

github-actions bot commented Nov 4, 2024

[puLL-Merge] - brave/brave-core@26343

Description

This PR makes several changes to the Brave Wallet codebase, primarily focusing on improving code safety and modernization by replacing raw pointers with references and using smart pointers where appropriate. It also includes some refactoring and minor adjustments to class interfaces.

Changes

Changes

  1. AssetDiscoveryManager:

    • Constructor now takes references instead of pointers for several parameters.
    • Member variables changed from raw pointers to references.
  2. AccountDiscoveryManager:

    • Constructor now takes references instead of pointers for JsonRpcService and KeyringService.
    • Member variables changed from raw pointers to references.
  3. AccountResolverDelegateImpl:

    • Constructor now takes a reference to KeyringService instead of a pointer.
    • Member variable changed from raw pointer to reference.
  4. AssetDiscoveryTask:

    • Constructor now takes references instead of pointers for several parameters.
    • Member variables changed from raw pointers to references.
  5. ZCashWalletService:

    • Constructor now takes a reference to KeyringService instead of a pointer.
    • zcash_rpc() and keyring_service() methods now return references instead of pointers.
    • complete_manager_ is now a direct member instead of a unique_ptr.
  6. ZCashTransactionCompleteManager:

    • Constructor now takes a reference to ZCashWalletService instead of a pointer.
    • Member variable changed from raw pointer to reference.
  7. ZCashTxManager:

    • Constructor now takes a reference to ZCashWalletService instead of a pointer.
    • Removed zcash_rpc_ member variable.
  8. Various unit tests have been updated to reflect these changes.

Possible Issues

  • The changes to constructor signatures and method return types may require updates in other parts of the codebase that are not shown in this diff.
  • There might be a need for additional null checks in some places where pointers were replaced with references, as references cannot be null.

Security Hotspots

No significant security hotspots were identified in this change. The move from raw pointers to references generally improves memory safety, which can indirectly improve security by reducing the risk of memory-related vulnerabilities.

@cdesouza-chromium cdesouza-chromium merged commit 5c49113 into master Nov 5, 2024
17 checks passed
@cdesouza-chromium cdesouza-chromium deleted the prefer-non-nullable-states-pt-2 branch November 5, 2024 11:05
@github-actions github-actions bot added this to the 1.73.x - Nightly milestone Nov 5, 2024
@brave-builds
Copy link
Collaborator

Released in v1.73.75

@bsclifton bsclifton modified the milestones: 1.73.x - Nightly, 1.74.x - Nightly Nov 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improve raw_ptr<T> uses around wallet
4 participants